package com.huaxin.hxmoduleworkorder.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaxin.hxmoduleworkorder.dto.ResponseResult;
import com.huaxin.hxmoduleworkorder.dto.request.InspectionOrderCustomerCreateReqDTO;
import com.huaxin.hxmoduleworkorder.dto.response.GetInspectionOrderCustomerRespDTO;
import com.huaxin.hxmoduleworkorder.mapper.InspectionOrderCustomerMapper;
import com.huaxin.hxmoduleworkorder.mapping.InspectionOrderCustomerMapping;
import com.huaxin.hxmoduleworkorder.models.InspectionOrderCustomer;
import com.huaxin.hxmoduleworkorder.service.InspectionOrderCustomerService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class InspectionOrderCustomerServiceImpl extends ServiceImpl<InspectionOrderCustomerMapper, InspectionOrderCustomer> implements InspectionOrderCustomerService {

    private final InspectionOrderCustomerMapping mapping;
    private final InspectionOrderCustomerMapper mapper;

    /**
     * 创建巡查工单客户关联
     *
     * @param in 创建参数
     * @return 创建结果
     */
    @Override
    public Boolean create(InspectionOrderCustomerCreateReqDTO in) {
        if (in == null) {
            return false;
        }
        if (StringUtils.isBlank(in.getOrderId())) {
            return false;
        }
        if (StringUtils.isBlank(in.getCustomerId())) {
            return false;
        }
        if (StringUtils.isBlank(in.getCustomerType())) {
            return false;
        }
        InspectionOrderCustomer inspectionOrderCustomer = mapping.toEntity(in);
        return save(inspectionOrderCustomer);
    }

    /**
     * 批量创建巡查工单客户关联
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchCreate(List<InspectionOrderCustomerCreateReqDTO> customerIds) {
        if (customerIds == null || customerIds.isEmpty()) {
            return false;
        }
        List<InspectionOrderCustomer> inspectionOrderCustomers = new ArrayList<>();
        for (InspectionOrderCustomerCreateReqDTO in : customerIds) {
            if (StringUtils.isBlank(in.getOrderId())) {
                return false;
            }
            if (StringUtils.isBlank(in.getCustomerId())) {
                return false;
            }
            if (StringUtils.isBlank(in.getCustomerType())) {
                return false;
            }
            inspectionOrderCustomers.add(mapping.toEntity(in));
        }
        return this.saveBatch(inspectionOrderCustomers);
    }

    /**
     * 删除巡查工单客户关联
     *
     * @param orderId    工单ID
     * @param customerId 客户ID
     * @return 删除结果
     */
    @Override
    public Boolean delete(String orderId, String customerId) {
        if (StringUtils.isBlank(orderId) || StringUtils.isBlank(customerId)) {
            return false;
        }
        return this.baseMapper.delete(orderId, customerId);
    }

    /**
     * 删除工单关联的工单客户
     *
     * @param orderId 工单ID
     * @return 删除结果
     */
    @Override
    public Boolean deleteByOrderId(String orderId) {
        if (StringUtils.isBlank(orderId)) {
            return false;
        }
        return this.baseMapper.deleteByOrderId(orderId);
    }

    /**
     * 获取巡查任务关联客户信息
     *
     * @param orderId 工单ID
     * @return 工单关联客户信息
     */
    @Override
    public ResponseResult<Page<GetInspectionOrderCustomerRespDTO>> pageList(Integer pageNum, Integer pageSize, String orderId) {
        if (StringUtils.isBlank(orderId)) {
            return ResponseResult.parameterError(null, "请选择工单！");
        }
        Page<GetInspectionOrderCustomerRespDTO> page = new Page<>(pageNum, pageSize);
        return ResponseResult.ok(mapper.getInspectionOrderCustomer(page, orderId));
    }
}
